//
//  CustomImagePicker.m
//  CustomImagePicker
//
//  Created by Ray Wenderlich on 1/27/10.
//  Copyright 2010 Ray Wenderlich. All rights reserved.
//

#import "CustomImagePicker.h"
#import "UIImageExtras.h"
#import "NewPicView.h"
#import "eyeinfoAppDelegate.h"
#import "RssItem.h"
#import "GlobalCls.h"
#import "NewWebView.h"

@implementation CustomImagePicker

@synthesize pageControl= m_pageControl;
@synthesize images = _images;
@synthesize thumbs = _thumbs;


- (id) init {
	if ((self = [super init])) {
		_images =  [[NSMutableArray alloc] init];
		_thumbs =  [[NSMutableArray alloc] init];
	}
	return self;
}

- (void)addImage:(UIImage *)image {
	[_images addObject:image];
	[_thumbs addObject:[image imageByScalingAndCroppingForSize:CGSizeMake(64, 64)]];
}

-(void) refrashImgViewData:(BOOL) bRefrashData
{
	self.navigationItem.rightBarButtonItem.title = [NSString stringWithFormat:@"刷新(%d)", m_nCurPage];
	eyeinfoAppDelegate* appDelegate = (eyeinfoAppDelegate*)[UIApplication sharedApplication].delegate;
	GetRssDataLogical* getRssDataLogic = appDelegate.m_getITRssLogic;
	if (bRefrashData == TRUE || getRssDataLogic.m_nRssImgPage != m_nCurPage)
	{
		getRssDataLogic.m_nRssImgPage = m_nCurPage;
		[getRssDataLogic createRssImgData];
	}
	[self refrashUIControl];
}

-(IBAction) handlePageChanged
{
	[self pageChangeClick];
	[self refrashImgViewData:FALSE];
}

-(void) pageChangeClick
{
	switch (m_pageControl.selectedSegmentIndex) {
		case 0:
			if (m_nCurPage > 1)
				m_nCurPage--;
			break;
		case 1:
			m_nCurPage++;
			break;
		default:
			break;
	}
	m_pageControl.selectedSegmentIndex = -1;
}

-(void) refrashUIControl
{
//	for(int i = 0; i < m_ImgPicView.count; ++i) {
//		NewPicView * button = [m_ImgPicView objectAtIndex:i];
//		[button removeFromSuperview]; 
//		[m_ImgPicView removeObject:button];
//	}

	
	int row = 0;
	int column = 0;

	
	eyeinfoAppDelegate* appDelegate = (eyeinfoAppDelegate*)[UIApplication sharedApplication].delegate;
	GetRssDataLogical* pGetRssDataLogic = appDelegate.m_getITRssLogic;
 	for (int i=0; i < pGetRssDataLogic.m_rssImglist.count; ++i) {
		RssItem* itemRss = [pGetRssDataLogic.m_rssImglist objectAtIndex:i];
		NewPicView* buttonNews;
		BOOL bIsCreate = FALSE;
		if (i >= m_ImgPicView.count)
		{
			bIsCreate = TRUE;
			buttonNews = [NewPicView buttonWithType:UIButtonTypeCustom];
			buttonNews.frame = CGRectMake(column*160+5, row*200, 157, 193);
			[buttonNews addTarget:self 
					   action:@selector(buttonClicked:) 
			 forControlEvents:UIControlEventTouchUpInside];
			buttonNews.tag = i; 
			[m_ImgPicView addObject:buttonNews];
		}
		else
			buttonNews =  [m_ImgPicView objectAtIndex:i];
		
		if (column == m_nColumnCount) {
			column = 0;
			row++;
		} else {
			column++;
		}
		
		buttonNews.imgTitle = itemRss.title;
		buttonNews.imgID = itemRss.n_id;
		buttonNews.imgUrl = itemRss.imglink;
		
		if (bIsCreate)
			[self.view addSubview:buttonNews];
		[buttonNews refrashView];

	} 

	[m_Scrollview setContentSize:CGSizeMake(m_nScreenWidth, (row+1) * 180 + 100)];
}


- (void)viewDidLoad {
	// Create view
	m_nCurPage = 1;
	
	CGRect rectScreen = [[UIScreen mainScreen] bounds];
	m_Scrollview = [[UIScrollView alloc] initWithFrame:rectScreen];
	
	m_nScreenWidth = rectScreen.size.width;
	m_nColumnCount = m_nScreenWidth / 155 - 1;
	
	self.view = m_Scrollview;
	
	// Create cancel button
	UIBarButtonItem *refrashButton = [[UIBarButtonItem alloc] 
									 initWithTitle:@"Cancel" 
									 style:UIBarButtonItemStylePlain 
									 target:self 
									 action:@selector(refrashData:)];
	self.navigationItem.rightBarButtonItem = refrashButton;
	[refrashButton release];
	
	[super viewDidLoad];

//	NewPicView* buttonNews = [NewPicView buttonWithType:UIButtonTypeCustom];
//	buttonNews.frame = CGRectMake(0*160+5, 0*200, 157, 193);
//	buttonNews.imgTitle = @"日立抢先发布3TB企业级硬盘 五碟装也可靠";
//	buttonNews.imgID = @"1095226582";
//	buttonNews.imgUrl = @"http://img.cnbeta.com/newsimg/110125/16203001681255175.png" ;
//	[buttonNews refrashView];
//	[m_Scrollview addSubview:buttonNews];
	
	[self refrashImgViewData:TRUE];
}

- (IBAction)buttonClicked:(id)sender {
	UIButton *button = (UIButton *)sender;

	NewWebView * newWebViewController = [[NewWebView alloc] initWithNibName:@"NewWebView" bundle:nil];
	eyeinfoAppDelegate* appDelegate = (eyeinfoAppDelegate*)[UIApplication sharedApplication].delegate;
	GetRssDataLogical* getRssDataLogic = appDelegate.m_getITRssLogic;
	RssItem * anRssItem = [getRssDataLogic.m_rssImglist objectAtIndex:button.tag];
	newWebViewController.m_rssItem = anRssItem;
	[[self navigationController] pushViewController:newWebViewController animated:YES];
	[newWebViewController release];	
}

- (IBAction)refrashData:(id)sender {
	[self refrashImgViewData:TRUE];
}

- (void)didReceiveMemoryWarning {
	[super didReceiveMemoryWarning];
}

- (void)viewDidUnload {
}

- (void)dealloc {
	self.images = nil;
	self.thumbs = nil;
	[m_Scrollview release];
	m_Scrollview = nil;
	[super dealloc];
}

@end
